let marks = [
  {
    czmObject: {
      xbsjType: "Pin",
      xbsjGuid: "85aef52c-33b5-4ee6-ad94-61ed190b1292",
      name: "员工中心",
      position: [2.1020750705468547, 0.6526003339561802, 72.2],
      pinBuilder: {},
      far: 1073741824
    }
  },
  {
    czmObject: {
      xbsjType: "Pin",
      xbsjGuid: "616196f9-1fe7-410c-907d-c526272e3f12",
      name: "密炼厂区",
      position: [2.101960905848174, 0.6525788989442391, 57.7],
      pinBuilder: {},
      far: 1073741824
    }
  },
  {
    czmObject: {
      xbsjType: "Pin",
      xbsjGuid: "798fb508-2b87-4e9e-a5a6-82f01eacc8f3",
      name: "PCR五厂",
      position: [2.102025032065021, 0.652643807603771, 34.61],
      pinBuilder: {},
      far: 1073741824
    }
  },
  {
    czmObject: {
      xbsjType: "Pin",
      xbsjGuid: "016a279f-f534-4f69-aca0-878720834172",
      name: "PCR二厂",
      position: [2.102038404842104, 0.6525968020749945, 25.66],
      pinBuilder: {},
      far: 1073741824
    }
  },
  {
    czmObject: {
      xbsjType: "Pin",
      xbsjGuid: "88994653-72b0-4b6a-8142-44dcc0ac0132",
      name: "电厂",
      position: [2.1020221570774344, 0.6525493930884796, 64.47],
      pinBuilder: {},
      far: 1073741824
    }
  },
  {
    czmObject: {
      xbsjType: "Pin",
      xbsjGuid: "156267ae-ff96-4cd4-a753-dbe2b36cdd33",
      name: "TBR三厂",
      position: [2.101914580354979, 0.6525686572066363, 30.87],
      pinBuilder: {},
      far: 1073741824
    }
  },
  {
    czmObject: {
      xbsjType: "Pin",
      xbsjGuid: "49564fb7-ee13-4460-98ea-adf1f21ee985",
      name: "TBR仓库",
      position: [2.1018231778166356, 0.6526213259502828, 27.89],
      pinBuilder: {},
      far: 1073741824
    }
  },
  {
    czmObject: {
      xbsjType: "Pin",
      xbsjGuid: "57e9d4d0-7267-44e6-9755-ca2f6aae6405",
      name: "TBR四厂",
      position: [2.1019219655401216, 0.652608564203782, 28.49],
      pinBuilder: {},
      far: 1073741824
    }
  },
  {
    czmObject: {
      xbsjType: "Pin",
      xbsjGuid: "2127ae28-0edc-4a7d-aff0-58154c079c52",
      name: "PCR六厂",
      position: [2.1019697249062625, 0.6526532128391391, 30.76],
      pinBuilder: {},
      far: 1073741824
    }
  },
  {
    czmObject: {
      xbsjType: "Pin",
      xbsjGuid: "52db5844-d904-4a83-9b79-a3ea946986d5",
      name: "PCR仓库",
      position: [2.1019974287701384, 0.6527177483415378, 31.368951303527453],
      pinBuilder: {},
      far: 1073741824
    }
  }
];

import uuidv1 from "uuid/v1";

let mixin = {
  props: {},
  mounted() {},
  data() {
    return {};
  },
  watch: {},

  methods: {
    ll() {
      this.loadtile();
      marks.map(item => {
        this.loadmark(item);
      });
    },

    loadtile() {
      this._earth.sceneTree.root.children.push({
        ref: "lltel",
        czmObject: {
          xbsjType: "Tileset",
          enabled: false,
          xbsjGuid: "8c237f67-8056-4f4e-80af-46c72824252a",
          name: "新建模型服务",
          // url: "http://127.0.0.1:8080/tileset.json",
          url: "http://124.70.158.145:9999/ll/tileset.json",
          show: true,
          maximumScreenSpaceError: 0.0625,
          maximumMemoryUsage: 5120,
          luminanceAtZenith: 5,
          xbsjPosition: [
            2.1019550073658775,
            0.6526472597922687,
            35
            // -4.920356252952729
          ],
          xbsjScale: [3, 3, 3],
          xbsjUseOriginTransform: false,
          xbsjStyle: 'var style = {\n    color: "vec4(0.29, 0.76, 1.0,1)"\n}',
          xbsjClippingPlanes: {},
          xbsjCustomShader: {
            fsBody: `
            float vtxf_a11 = fract(czm_frameNumber / 120.0) * 3.14159265 * 0.5;
            float vtxf_a12 = v_elevationPos.z / 200.0 + sin(vtxf_a11) * 0.5;
            gl_FragColor *= vec4(vtxf_a12, vtxf_a12, vtxf_a12, 1.0);

            float vtxf_a13 = fract(czm_frameNumber / 180.0);
            float vtxf_h = clamp(v_elevationPos.z / 180.0, 0.0, 1.0);
            vtxf_a13 = abs(vtxf_a13 - 0.5) * 2.0;
            float vtxf_diff = step(0.005, abs(vtxf_h - vtxf_a13));
            gl_FragColor.rgb += gl_FragColor.rgb * (1.0 - vtxf_diff);
            `
          }
        }
      });

      this._earth.interaction.picking.enabled = true;
      this._earth.interaction.picking.clickedColor = [0.239, 0.823, 0.709, 1];

      const tileset1 = this._earth.sceneTree.$refs.lltel.czmObject;

      // Cesium后处理
      var silhouetteBlue = Cesium.PostProcessStageLibrary.createEdgeDetectionStage();
      silhouetteBlue.uniforms.color = Cesium.Color.BLUE;
      silhouetteBlue.uniforms.length = 0.01;
      silhouetteBlue.selected = [];

      var silhouetteGreen = Cesium.PostProcessStageLibrary.createEdgeDetectionStage();
      silhouetteGreen.uniforms.color = Cesium.Color.LIME;
      silhouetteGreen.uniforms.length = 0.01;
      silhouetteGreen.selected = [];

      this._earth.czm.scene.postProcessStages.add(
        Cesium.PostProcessStageLibrary.createSilhouetteStage([
          silhouetteBlue,
          silhouetteGreen
        ])
      );

      // 拾取事件定制
      tileset1.onclick = pickedObject => {
        // pickedObject.getProperty('id');
        // pickedObject.getProperty('name');

        if (
          !silhouetteBlue.selected ||
          silhouetteBlue.selected[0] !== pickedObject
        ) {
          silhouetteBlue.selected = [pickedObject];
          
        }


        this.cardnengliuarr.filter(item => { 
          return item.options.id == pickedObject.getProperty("name");
        })

        if (
          this.cardnengliuarr.filter(item => {
            return item.options.id == pickedObject.getProperty("name");
          }).length != 0
        ) {
          return
        }


        this.$message.success(pickedObject.getProperty("name"));

        let positionarr = marks.filter(mark => { 
          return (mark.czmObject.name == pickedObject.getProperty("name"));
        })

        let options = {
          id: pickedObject.getProperty("name"),
          position: positionarr[0].czmObject.position
        };

        let uniqueID = uuidv1();
        options.uid = uniqueID;
        this.cardnengliuarr.push({
          uid: uniqueID,
          diahtml: "",
          options: options
        });

        // return false; 是否禁用点选效果？
      };

      tileset1.onclickout = () => {
        if (silhouetteBlue.selected && silhouetteBlue.selected.length > 0) {
          silhouetteBlue.selected = [];
        }

        // return false; 是否禁用点选效果？
      };

      tileset1.onmouseover = pickedObject => {
        if (
          !silhouetteGreen.selected ||
          silhouetteGreen.selected[0] !== pickedObject
        ) {
          silhouetteGreen.selected = [pickedObject];
        }
        // return false; 是否禁用点选效果？
      };

      tileset1.onmouseout = () => {
        if (silhouetteGreen.selected && silhouetteGreen.selected.length > 0) {
          silhouetteGreen.selected = [];
        }
        // return false; 是否禁用点选效果？
      };
    },

    loadmark(item) {
      let ppp = _.cloneDeep(item.czmObject.position);
      // ppp[0] = (((ppp[0] * 180.0) / Math.PI - 0.001) * Math.PI) / 180.0;
      let ddd = {
        czmObject: {
          xbsjType: "CustomPrimitive",
          xbsjGuid: "f65e1f32-ee82-4ec6-82e3-50d4a2d3f1bf",
          evalString: `
          Cesium.Resource.createIfNeeded('/configuration/cesium/lllabel.png').fetchImage().then(function(image) {
            p.drawCanvas(ctx => {
              ctx.clearRect(0, 0, 329, 165);
              ctx.font='50px Arial';
              ctx.drawImage(image, 0, 0);
              ctx.fillStyle='#eeeeeeee';
              ctx.font='50px Arial';
              ctx.fillText("${item.czmObject.name}", 100, 70);
              ctx.fillText("${_.random(0, 10000)}", 100, 130);
            });
          });`,

          // evalString:
          //   "\n                Cesium.Resource.createIfNeeded('/configuration/cesium/lllabel.png').fetchImage().then(function(image) {\n                    console.log('image loaded!');\n                    p.drawCanvas(ctx => {\n                        ctx.clearRect(0, 0, 329, 165);ctx.font='50px Arial';\n                        ctx.drawImage(image, 0, 0);ctx.fillStyle='#eeeeeeee';ctx.font='50px Arial';ctx.fillText('" +
          //   item.czmObject.name +
          //   "', 100, 70);ctx.fillText('" +
          //   _.random(0, 10000) +
          //   "', 100, 130);\n                    });\n                });\n            ",
          position: ppp,
          // position: item.czmObject.position,
          // rotation: [],
          rotation: [1.8, 0, 0],
          scale: [10, 80, 40],
          positions: [0, -1, 0, 0, 1, 0, 0, 1, 2, 0, -1, 2],
          sts: [0, 0, 1, 0, 1, 1, 0, 1],
          indices: [0, 1, 2, 0, 2, 3],
          renderState: {
            cull: {
              enabled: false
            },
            polygonOffset: {},
            scissorTest: {
              rectangle: {}
            },
            depthRange: {},
            depthTest: {
              enabled: true
            },
            colorMask: {},
            depthMask: false,
            blending: {
              enabled: true,
              color: {},
              equationRgb: 32774,
              equationAlpha: 32774,
              functionSourceRgb: 770,
              functionDestinationRgb: 771,
              functionDestinationAlpha: 771
            },
            stencilTest: {
              frontOperation: {},
              backOperation: {}
            },
            sampleCoverage: {}
          },
          color: [0.5, 0.8, 1, 2],
          canvasWidth: 329,
          canvasHeight: 165,
          autoRegisterEditing: true
        }
      };

      this._earth.sceneTree.root.children.push(ddd);

      let options = {
        id: item.czmObject.name,
        position: item.czmObject.position
      };

      let uniqueID = uuidv1();
      options.uid = uniqueID;
      this.cardmsgarr.push({
        uid: uniqueID,
        diahtml: "",
        options: options
      });

    }
  }
};

export default mixin;
